package meconsea.webcoll.sys.web;

import meconsea.webcoll.sys.dao.SysUserDao;
import meconsea.webcoll.sys.dao.SysUserPopedomDao;
import meconsea.webcoll.sys.entity.SysUser;
import meconsea.webcoll.sys.entity.SysUserPopedom;
import meconsea.webcoll.util.ComUtil;
import meconsea.webcoll.util.ListRange;
import meconsea.webcoll.util.WebCollConstant;
import meconsea.webcoll.util.web.BaseAction;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.google.inject.Inject;

public class LoginAction extends BaseAction {
	private static final Logger log = Logger.getLogger(LoginAction.class);
	
	private String loginName;
	private String password;
	private String psnCard;
	private String popedom;
	
	@Inject
	private SysUserDao sysUserDao;
	
	@Inject
	private SysUserPopedomDao sysUserPopedomDao;
	
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPsnCard() {
		return psnCard;
	}

	public void setPsnCard(String psnCard) {
		this.psnCard = psnCard;
	}
	
	public String getPopedom() {
		return popedom;
	}

	public void setPopedom(String popedom) {
		this.popedom = popedom;
	}

	public String doInit(){
		return "login";
	}
	public String doLogin(){
		ListRange<SysUser> lr = new ListRange<SysUser>();
		SysUser su = sysUserDao.findSULoginName(loginName);
		if(su == null){
			lr.setSuccess(false);
			lr.setMessage("用户名不存在!");
			outJson(lr);
			return null;
		}
		if(!psnCard.equals(su.getPsnCard())){
			lr.setSuccess(false);
			lr.setMessage("身份认证号码不对!");
			outJson(lr);
			return null;
		}
		if(!password.equals(ComUtil.md5(password+"webcoll"+psnCard))){
			
		}
		//获取用户的权限，并将用户加入session
		SysUserPopedom sup = sysUserPopedomDao.getSysUserPopedom(su.getId());
		if(sup != null && sup.getPopedomInfo() != null && sup.getPopedomInfo().length()>1){
			setPopedom(sup.getPopedomInfo());
			this.session.put(WebCollConstant.ADMIN_USER, su);
			return this.SUCCESS;
		}else{
			lr.setSuccess(false);
			lr.setMessage("权限认证失败!");
			outJson(lr);
			return null;
		}
	}
	
}
